package com.lightsh.provider;

import com.ligthsh.db.MyDbHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class MyContentProvider extends ContentProvider{

	private static final boolean DEBUG = true;
	private static final String TAG = "MyContentProvider";
	
	public final static String AUTHORITY = "com.lightsh.content";
	
	public final static String TABLE_NAME = MyDbHelper.TABLE_NAME;
	
	public final static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
	private static final int TYPE_TABLE_ALL = 1;
	private static final int TYPE_TABLE_ID = 2;
	private final static UriMatcher URI_MATCHER;
	static {
		URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URI_MATCHER.addURI(AUTHORITY, TABLE_NAME, TYPE_TABLE_ALL);
		URI_MATCHER.addURI(AUTHORITY, TABLE_NAME + "/#", TYPE_TABLE_ID);
	}
	
	private void notifyChange(){
		getContext().getContentResolver().notifyChange(CONTENT_URI, null);
	}
	private void notifyChange(Uri uri){
		getContext().getContentResolver().notifyChange(uri, null);
	}	
	private MyDbHelper mDbHelper;
	
	
	
	@Override
	public boolean onCreate() {
		mDbHelper = new MyDbHelper(getContext());
		return true;
	}
	
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int num = 0;
		switch(URI_MATCHER.match(uri)){
		case TYPE_TABLE_ALL:{
			num =  mDbHelper.delete(TABLE_NAME, selection, selectionArgs);
		}
			break;
		case TYPE_TABLE_ID:{
			long id = ContentUris.parseId(uri);
			String where = MyDbHelper.FIELD_ID + "=" + id;
			if (selection != null && !"".equals(selection.trim())) {
				where = where + " and " + selection;
			}
			num = mDbHelper.delete(TABLE_NAME, where, selectionArgs);
		}
			break;
			default:
				throw new IllegalArgumentException("Unknown Uri:" + uri);
		}		
		notifyChange(uri);
		return num;
	}

	@Override
	public String getType(Uri uri) {
		switch(URI_MATCHER.match(uri)){
		case TYPE_TABLE_ALL:{
			return "vnd.android.cursor.dir/" + TABLE_NAME;
		}
		case TYPE_TABLE_ID:{
			return "vnd.android.cursor.item/" + TABLE_NAME;
		}
		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowId = 0;
		switch(URI_MATCHER.match(uri)){
		case TYPE_TABLE_ALL:{
			// 插入时只允许该uri调用
			rowId =  mDbHelper.insert(TABLE_NAME, MyDbHelper.FIELD_ID, values);
		}
			break;
			default:
				throw new IllegalArgumentException("Unknown Uri:" + uri);
		}		
		
		notifyChange();
		return ContentUris.withAppendedId(uri, rowId);
	}
	

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		switch(URI_MATCHER.match(uri)){
		case TYPE_TABLE_ALL:{
			return mDbHelper.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
		}
		case TYPE_TABLE_ID:{
			long id = ContentUris.parseId(uri);
			String where = MyDbHelper.FIELD_ID + "=" + id;
			if (selection != null && !"".equals(selection.trim())) {
				where = where + " and " + selection;
			}
            return mDbHelper.query(TABLE_NAME, projection, where, selectionArgs, null, null, sortOrder);
		}
			default:
				throw new IllegalArgumentException("Unknown Uri:" + uri);
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int num = 0;
		switch(URI_MATCHER.match(uri)){
		case TYPE_TABLE_ALL:{
			num =  mDbHelper.update(TABLE_NAME, values, selection, selectionArgs);
		}
			break;
		case TYPE_TABLE_ID:{
			long id = ContentUris.parseId(uri);
			String where = MyDbHelper.FIELD_ID + "=" + id;
			if (selection != null && !"".equals(selection.trim())) {
				where = where + " and " + selection;
			}
			num = mDbHelper.update(TABLE_NAME, values, where, selectionArgs);
		}
			break;
			default:
				throw new IllegalArgumentException("Unknown Uri:" + uri);
		}		
		notifyChange(uri);
		return num;
	}

}
